package com.myapp.ruyixing.ui;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.WholeCity;
import com.myapp.ruyixing.model.WholeCity.WholeCityCell;
import com.myapp.ruyixing.ui.widget.TitleBar;

public class SearchCityActivity extends BaseActivity implements OnClickListener {

	private View[] districtViews;
	private View[] categoryViews;

	private RYHttpResponseHandler mRyHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {

			findViewById(R.id.body).setVisibility(View.VISIBLE);

			int index = 1;
			WholeCity city = (WholeCity) o;
			if (city.districts != null) {
				int size = city.districts.size();

				if (size < 8) {
					findViewById(R.id.district_row2).setVisibility(View.GONE);
					for (int i = 4; i < 8; i++) {
						districtViews[i] = null;
					}
					if (size < 4) {
						findViewById(R.id.district_row1).setVisibility(
								View.GONE);
						for (int i = 0; i < 4; i++) {
							districtViews[i] = null;
						}
					}
				}

				for (View v : districtViews) {
					if (v != null && index < 12) {
						if (index < city.districts.size()) {
							((TextView) v)
									.setText(city.districts.get(index).name);
							((TextView) v).setTag(city.districts.get(index));
						} else {
							((TextView) v).setVisibility(View.GONE);
						}
						index++;
					}
				}
			}

			index = 1;

			if (city.categories != null) {
				for (View v : categoryViews) {
					if (index < 12) {
						if (index < city.categories.size()) {

							((TextView) v)
									.setText(city.categories.get(index).name);
							((TextView) v).setTag(city.categories.get(index));
						} else {
							((TextView) v).setVisibility(View.GONE);
						}
					}
					index++;
				}
			}
			findViewById(R.id.district_more).setVisibility(View.VISIBLE);
		}

		@Override
		public void onFailure(Throwable throwable) {
			Toast.makeText(getApplicationContext(), throwable.getMessage(),
					Toast.LENGTH_SHORT).show();
		}
	};

	private OnClickListener mDistrictViewClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			WholeCityCell cell = (WholeCityCell) v.getTag();
			if (cell != null) {
				startActivity(new Intent(SearchCityActivity.this,
						NearbyStoreActivity.class).putExtra(
						NearbyStoreActivity.INTENT_KEY_DISTIRCTID, cell.id)
						.putExtra(NearbyStoreActivity.INTENT_KEY_DISTIRCT_NAME,
								cell.name));
			}
		}
	};

	private OnClickListener mCategoryViewClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			WholeCityCell cell = (WholeCityCell) v.getTag();
			if (cell != null) {
				startActivity(new Intent(SearchCityActivity.this,
						NearbyStoreActivity.class).putExtra(
						NearbyStoreActivity.INTENT_KEY_TYPEID, cell.id)
						.putExtra(NearbyStoreActivity.INTENT_KEY_TYPE_NAME,
								cell.name));
			}
		}
	};

	@Override
	protected void onCreate(android.os.Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_search_city);

		initViews();

		RYHttpClient.getWholeCity(this, mRyHttpResponseHandler);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		RYHttpClient.cancleRequest(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.district_more:
			startActivity(new Intent(this, SearchCityDistrictActivity.class));

			break;
		case R.id.category_more:
			startActivity(new Intent(this, SearchCityMenuActivity.class));

			break;

		case R.id.img_icon:
			finish();
			break;

		default:
			break;
		}
	}

	private void initViews() {
		districtViews = new View[12];
		districtViews[0] = findViewById(R.id.district1);
		districtViews[1] = findViewById(R.id.district2);
		districtViews[2] = findViewById(R.id.district3);
		districtViews[3] = findViewById(R.id.district4);
		districtViews[4] = findViewById(R.id.district5);
		districtViews[5] = findViewById(R.id.district6);
		districtViews[6] = findViewById(R.id.district7);
		districtViews[7] = findViewById(R.id.district8);
		districtViews[8] = findViewById(R.id.district9);
		districtViews[9] = findViewById(R.id.district10);
		districtViews[10] = findViewById(R.id.district11);
		districtViews[11] = findViewById(R.id.district_more);

		categoryViews = new View[12];
		categoryViews[0] = findViewById(R.id.category1);
		categoryViews[1] = findViewById(R.id.category2);
		categoryViews[2] = findViewById(R.id.category3);
		categoryViews[3] = findViewById(R.id.category4);
		categoryViews[4] = findViewById(R.id.category5);
		categoryViews[5] = findViewById(R.id.category6);
		categoryViews[6] = findViewById(R.id.category7);
		categoryViews[7] = findViewById(R.id.category8);
		categoryViews[8] = findViewById(R.id.category9);
		categoryViews[9] = findViewById(R.id.category10);
		categoryViews[10] = findViewById(R.id.category11);
		categoryViews[11] = findViewById(R.id.category_more);

		int index = 1;
		for (View v : districtViews) {
			if (index < 12) {
				v.setOnClickListener(mDistrictViewClickListener);
			} else {
				v.setOnClickListener(this);
			}
			index++;
		}
		index = 1;
		for (View v : categoryViews) {
			if (index < 12) {
				v.setOnClickListener(mCategoryViewClickListener);
			} else {
				v.setOnClickListener(this);
			}
			index++;
		}

		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(this);

		findViewById(R.id.body).setVisibility(View.GONE);
	}
}
